package org.example.day;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/20 20:32
 */
public class Solution283 {

    public static void main(String[] args) {
        Solution283 solution = new Solution283();
        int[] nums = new int[]{4,2,4,0,0,3,0,5,1,0};
        solution.moveZeroes(nums);
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }

    public void moveZeroes(int[] nums) {
        if(nums.length == 1){
            return;
        }
        int next;

        for (int i = 0; i < nums.length-1; i++) {
            if(nums[i] != 0){
                continue;
            }

            next = i + 1;

            while(next < nums.length-1 && nums[next] == 0){
                next++;
            }

            int temp = nums[i];
            nums[i] = nums[next];
            nums[next] = temp;
        }
    }


    public void moveZeroes2(int[] nums) {
        if(nums==null) {
            return;
        }
        //两个指针i和j
        int j = 0;
        for(int i=0;i<nums.length;i++) {
            //当前元素!=0，就把其交换到左边，等于0的交换到右边
            if(nums[i]!=0) {
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j++] = tmp;
            }
        }
    }
}
